﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Service.Forms
{
    public partial class CustomQueryForm : BaseForm
    {
        public CustomQueryForm()
        {
            InitializeComponent();

            InitializeBinding();
        }

        public override void InitializeBinding()
        {
            Fis.Win.Utils.UIUtils.LookUpBinding(edState, Fis.Win.Utils.AppSession.StateCache(), "display", "value");
        }

        public string Filter
        {
            get
            {
                var f = new StringBuilder("1=1");

                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edName, " AND FENT like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edRegiste, " AND FREGISTRE like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edPerson, " AND FCONTRACT like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edPassport, " AND FPASSPORT like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edTel, " AND FTEL like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edMobile, " AND FEMAIL like '%{0}%'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edState, " AND FSTATE = {0}"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edEnterS, " AND FENTERTIME >= '{0}'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edEnterE, " AND FENTERTIME <= '{0}'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edAuditS, " AND FAUDITIME >= '{0}'"));
                f.Append(Fis.Win.Utils.UIUtils.ToFilterString(edAuditE, " AND FAUDITIME <= '{0}'"));

                return f.ToString();
            }
        }

        bool isLocked = false;
        private void DateEnd_EditValueChanged(object sender, EventArgs e)
        {
            if (isLocked)
                return;
            isLocked = true;

            var edit = sender as DevExpress.XtraEditors.DateEdit;
            if (edit != null)
            {
                edit.EditValue = ((DateTime)edit.EditValue).AddDays(1).AddSeconds(-1);
                edit.DoValidate();
            }

            isLocked = false;
        }

        private void edState_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Close)
            {
                edState.EditValue = null;
            }
        }

        private void bnOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void bnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
    }
}
